<?php
class loginControl extends skymvc{
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onDefault(){
		if(SHOPID){
			$this->gourl("/koudaiadmin.php?m=index");
		}
		$this->smarty->display("login/login.html");
	}
	
	public function onSave(){
		$adminname=post('adminname','h');
		$row=M("koudai_admin")->selectRow(array("where"=>" adminname='".$adminname."' "));
		if($row && $row['password']==umd5(post('password').$row['salt'])){
			$_SESSION['sskoudaiadmin']=$row;
			$authcode=jiami($row['adminid']."|".umd5($row['password']));
			setcookie("kd_authcode",$authcode,time()+3600000,"/",DOMAIN);
			//处理推送相关
			M("apppush")->add("koudaiadmin",$row);
			$this->goall("登录成功",0,0,APPKOUDAI."?m=iframe");
		}else{
			$this->goall("账户或者密码出错",1);
		}
	}
	
	public function onlogout(){
		$_SESSION['sskoudaiadmin']="";
		 setcookie("kd_authcode","",time()-3999,"/",DOMAIN);	
		$this->goall("退出成功",0,0,APPKOUDAI."/index.php?m=login");
	}
	
	
	
	public function onAdmin(){
		M("login")->checkAdminLogin();
		$id=get('id','i');
		$admin=M("koudai_admin")->selectRow(array("where"=>"shopid=".$id." AND typeid=1 "));
		$koudai=M("koudai")->selectRow("id=".$id);
		if(empty($admin)){
			M("koudai_admin")->insert(array(
					"salt"=>1234,
					"shopid"=>$id,
					"adminname"=>$id."@admin@.com",
					"password"=>umd5(time()),
					"typeid"=>1,
					"siteid"=>$koudai['siteid']
				));
			$admin=M("koudai_admin")->selectRow(array("where"=>"shopid=".$id." AND typeid=1 "));	
		} 
		if($admin['siteid']!=$koudai['siteid']){
			M("koudai_admin")->update(array(
				"siteid"=>$koudai['siteid']
			),"adminid=".$admin['adminid']);
			$admin['siteid']=$koudai['siteid'];
		}
			$_SESSION['sskoudaiadmin']=$admin;
		 
			$this->gourl(APPKOUDAI."?m=iframe"); 
	}
		
}
?>